﻿#pragma once
struct Point
{
	double longitude;
	double latitude;
	int node_id=0;
	int list_id = 0;
	int people_type_weight = 0;
	void print()
	{
		printf("%.8f,%.8f,node_id %d list %d\n", longitude, latitude, node_id, list_id);
	}
	double calZDis(Point &p2)
	{
		double distance2 = fabs((this->longitude - p2.longitude)) + fabs(this->latitude - p2.latitude);
		return distance2;
	}
};
class Gen_Path {
public:
	vector<Point> peoples_point[3];
	vector<Point> ALL_peoples_points;
	Gen_Path()
	{
		ReadALLpoeple("C:\\Users\\tang\\Desktop\\RHZT\\R1\\subject_2\\data\\client_path_1.json");
	}
	int is_in_nodes(int index)
	{
		for (int V = 0; V < ALL_peoples_points.size(); V++)
		{
			Point* p1 = &ALL_peoples_points[V];//V为边序号ID graph.edgemap1[V]值为节点ID
			//Node* p2 = &pnodes[edgemap2[V]];
			if (index == p1->node_id)
			{
				return V;
			}
			/*if(p1->index)*/
			//graph.weight[V] = p1->GetDistance(p2);
			//string name(to_string(graph.edgemap1[V]) + "-" + to_string(graph.edgemap2[V]));
			//graph.allEdgeswight[name] = graph.weight[V];
		}
		//cout << endl;
		return -1;
	}

	void gen_point_id(  Graph &graph)
	{
		//Point p4;
		for (int i = 0; i < 3; i++)
		{

			for (auto& p0 : peoples_point[i])
			{
				int min_id = 0;
				double min_val = 1000;
				double cal_val = 1000;
				Point p3;
				for (int V = 0; V < graph.edge_max_num; V++)
				{
					Node* p1 = &graph.pnodes[graph.edgemap1[V]];//V为边序号ID graph.edgemap1[V]值为节点ID
					p3.latitude = p1->latitude;
					p3.longitude = p1->longitude;
					cal_val = p0.calZDis(p3);
					if (cal_val < min_val)
					{
						p0.node_id = graph.edgemap1[V];
						min_id = graph.edgemap1[V];
						min_val = cal_val;
					}
				}
				//printf("cal_val=%.8f,min_id %d\n", cal_val, min_id);
				//p0.print();
				//graph.pnodes[min_id].print("");
			}
		}
	}
	void gen_point_id(Graph& graph, vector<Point> &points)
	{

			for (auto& p0 : points)
			{
				int min_id = 0;
				double min_val = 1000;
				double cal_val = 1000;
				Point p3;
				for (int V = 0; V < graph.edge_max_num; V++)
				{
					Node* p1 = &graph.pnodes[graph.edgemap1[V]];//V为边序号ID graph.edgemap1[V]值为节点ID
					p3.latitude = p1->latitude;
					p3.longitude = p1->longitude;
					cal_val = p0.calZDis(p3);
					if (cal_val < min_val)
					{
						p0.node_id = graph.edgemap1[V];
						min_id = graph.edgemap1[V];
						min_val = cal_val;
					}
				}
			}
	}

	void Readpoeple(string strXmlPath)
	{
		std::string line, jsonStr;
		std::ifstream ifs(strXmlPath.c_str());
		while (getline(ifs, line))
		{
			jsonStr.append(line);
		}
		ifs.close();
		bool res;
		JSONCPP_STRING errs;
		Json::Value root, rows, data;
		Json::CharReaderBuilder readerBuilder;
		std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
		res = jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &root, &errs);
		if (!res || !errs.empty())
		{
			std::cout << "ReadJsonFromString error! " << errs << std::endl;
		}
		int countpoint = 0;
		for (int i = 0; i < root["car_points"].size(); i++)
		{
			Point p1;
			p1.latitude = root["car_points"][i]["start_point"]["latitude"].asDouble();
			p1.longitude = root["car_points"][i]["start_point"]["longitude"].asDouble();
			p1.list_id = countpoint;
			
			peoples_point[i].push_back(p1);
			countpoint++;
			for (int j = 0; j < root["car_points"][i]["pass_points"].size(); j++) //
			{
				Point p2;
				p2.latitude = root["car_points"][i]["pass_points"][j]["latitude"].asDouble();
				p2.longitude = root["car_points"][i]["pass_points"][j]["longitude"].asDouble();
				p2.list_id = countpoint++;
				peoples_point[i].push_back(p2);
				//p2.print();
			}
		}

	}
	void ReadALLpoeple(string strXmlPath)
	{
		std::string line, jsonStr;
		std::ifstream ifs(strXmlPath.c_str());
		while (getline(ifs, line))
		{
			jsonStr.append(line);
		}
		ifs.close();
		bool res;
		JSONCPP_STRING errs;
		Json::Value root, rows, data;
		Json::CharReaderBuilder readerBuilder;
		std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
		res = jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &root, &errs);
		if (!res || !errs.empty())
		{
			std::cout << "ReadJsonFromString error! " << errs << std::endl;
		}
		int countpoint = 0;

			for (int j = 0; j < root["rescue_people"].size(); j++) //
			{
				Point p2;
				p2.latitude = root["rescue_people"][j]["people_point"]["latitude"].asDouble();
				p2.longitude = root["rescue_people"][j]["people_point"]["longitude"].asDouble();
				p2.people_type_weight = root["rescue_people"][j]["people_type"].asInt();
				p2.list_id = countpoint++;
				
				ALL_peoples_points.push_back(p2);
				//p2.print();
			}
			cout << "ALL_peoples_points:" << ALL_peoples_points.size() << endl;

	}
};
